#--clear_datastore
from rer import SampleData, Categories
from django.utils import simplejson
from google.appengine.ext import db, webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class ImportData(webapp.RequestHandler):
    def get(self):
        f = open('quickref.json')
        json = simplejson.load(f)
        data = json['ref']
        i, n, ckey = 0, 0, None
        for dt in data:
            c = dt['category']
            ctgry = Categories(index=i, en = c['en'], ja = c['ja'])
            ctgry.put()
            i += 1
            ckey = ctgry.key()
            items = dt['items']
            for item in items:
                p, t, d = item['pattern'], item['title'], item['desc']
                smpl = SampleData(index = n,
                                  category = ckey,
                                  pattern = db.Text( p if p != '' else '-' ),
                                  title = db.Text( t if t != '' else '-' ),
                                  description = db.Text( d if d != '' else '-' ))
                smpl.put()
                n += 1
        
        self.redirect('/reference')



application = webapp.WSGIApplication([('/import_data', ImportData)], debug=False)

def main():
    run_wsgi_app(application)

if __name__ == '__main__':
    main()